package com.dyh.quickdemozwei.cmdlevel;



import lombok.Getter;
import lombok.Setter;
import org.junit.jupiter.api.Test;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Setter@Getter
public class CmdObject {

    Map<Class, List<Component>> mClsComponet = new HashMap<>();
    String bizType;

    void addComponent(Component component){
        mClsComponet.computeIfAbsent(component.getClass(), k -> new ArrayList<>());
        mClsComponet.get(component.getClass()).add(component);
    }

    void setInput(Object input){
        List<Component> components = mClsComponet.get(CmdObject.InputComponent.class);
        if (components==null){
           throw new UnsupportOperationException("没有input组件");
        }
        ((InputComponent)components.get(0)).setInput(input);
    }
    Object getInput(){
        List<Component> components = mClsComponet.get(CmdObject.InputComponent.class);
        if (components==null){
            throw new UnsupportOperationException("没有input组件");
        }
        return ((InputComponent)components.get(0)).getInput();
    }
    /*---------------------*/

    static class UnsupportOperationException extends RuntimeException{
        public UnsupportOperationException(String message) {
            super(message);
        }
    }

    interface Component{

    }
    @Setter@Getter
    static class InputComponent implements Component{
        Object input;


    }

    @Setter@Getter
    static class OutputComponent implements Component{
        Object output;
    }

    static class SerializeComponent implements Component{

    }

    public static void main(String[] args) {

        Map<String, Consumer<CmdObject>> map = new HashMap<>();
        map.put("aa",e-> System.out.println(e.getInput()));

        CmdObject cmdObject = new CmdObject();
        cmdObject.addComponent(new InputComponent());
        cmdObject.addComponent(new SerializeComponent());
        cmdObject.setInput("123");
        cmdObject.setBizType("aa");

        map.get(cmdObject.bizType).accept(cmdObject);
    }


    @Test
    public void test(){
        List<BigDecimal> skuPrices = new ArrayList<>();
        BigDecimal maxSkuPrice=skuPrices.stream().max(BigDecimal::compareTo).orElse(BigDecimal.ZERO);

        System.out.println();
        /*\
        java.util.NoSuchElementException: No value present

	at java.util.Optional.get(Optional.java:135)
	at com.dyh.quickdemozwei.cmdlevel.CmdObject.test(CmdObject.java:87)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
        */
    }


}
